﻿  $(document).ready(function() {
        var content = $('#kodEDRPOY').val();
            if (content.length > 0) {
                var url = "/AjaxQuery/GetFirmName?code=" + content;
                $.get(url, function (content) {
                    $("#nazva").val(content);
                });

                var url = "/AjaxQuery/GetFirmAddress?code=" + content;
                $.get(url, function (content) {
                    $("#adresa").val(content);
                });
                }
});
//---------NumberOnly-------------*@
  $(function() {
    $(".number").keydown(function(event) {
         // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 ) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            } 
        }
    });
//---------PostqueryForVatCode-------------*@
    $('#kodEDRPOY').keyup(function() { 
        var content = $('#kodEDRPOY').val();
            if (content.length > 0) {
                var url = "/AjaxQuery/GetFirmName?code=" + content;
                $.get(url, function (content) {
                    $("#nazva").val(content);
                });

                var url = "/AjaxQuery/GetFirmAddress?code=" + content;
                $.get(url, function (content) {
                    $("#adresa").val(content);
                });

            }
    });
});
//-----------DatePicker------------*@
  $(function () {
            $(".datedoc").datepicker({
                 defaultDate: '01-01-1985',
                 changeYear: true,
                  yearRange: '1920:'+new Date().toDateString(),
            });
        });
//--------AutoCompleter-----------*@
  $(function() {
     //Region
     $(".region").autocomplete({
     source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchRegions', type: "POST", dataType: "json",
                    data: {
                        term: request.term
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                         value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdnreg").val(ui.item.id);
            }
});

     //District
     $(".district").autocomplete({
     source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchDistricts', type: "POST", dataType: "json",
                    data: {
                        term: request.term,
						regionID:   $("#hdnreg").val()
					} ,
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                               value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdndist").val(ui.item.id);
            }
});
 
     //City
	 $(".city").autocomplete({
	source: function( request, response ) {
				$.ajax({
					  url:  '/AjaxQuery/SearchCities', type: "POST", dataType: "json",
					data: {
                        term: request.term,
						districtID:   $("#hdndist").val()
					} ,
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdncity").val(ui.item.id);

            }
		});

     //Street
	 $(".street").autocomplete({
	 source: function( request, response ) {
				$.ajax({
					  url:  '/AjaxQuery/SearchStreets', type: "POST", dataType: "json",
					data: {
                        term: request.term,
						cityID:   $("#hdncity").val()
					},
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
            minLength: 2
		});
    //Person LastName
    $("#lastname").autocomplete({
       source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchPhisicalPersonLastName', type: "POST", dataType: "json",
                    data: {
                        term: request.term
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item
                            };
                        }));
                    } 
                });
            },
            minLength: 2 
    });
     //Person Name
        $("#name").autocomplete({
       source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchPhisicalPersonName', type: "POST", dataType: "json",
                    data: {
                        term: request.term,
                        lastname: $("#lastname").val()
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item
                            };
                        }));
                    } 
                });
            }
    });
    //Person Surname
     $("#surname").autocomplete({
	source: function( request, response ) {
				$.ajax({
					  url:  '/AjaxQuery/SearchPhisicalSurname', type: "POST", dataType: "json",
					data: {
                        term: request.term,
						lastname: $("#lastname").val(),
                        name: $("#name").val(),
					} ,
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                          
                                value: item.Surname,
                                Surname:item.Surname,
                                PersonBirthdayYear: item.PersonBirthdayYear,
                                PersonPhoneNumber:item.PersonPhoneNumber,
                                PersonPostIndex:item.PersonPostIndex,
                                PersonIssuedBy: item.PersonIssuedBy,
                                PersonNumberСertificate: item.PersonNumberСertificate,
                                PersonDateOfIssueСertificate:  item.PersonDateOfIssueСertificate,
                                PersonSeriesСertificate:item.PersonSeriesСertificate,
                                PersonRegion:item.PersonRegion,
                                PersonDistrict:item.PersonDistrict,
                                PersonCity:item.PersonCity,
                                PersonStreet:item.PersonStreet,
                                PersonBuild:item.PersonBuild,
                                PersonApartment:item.PersonApartment
                            };
                        }));
                    }
                });
            },
          select: function(event, ui) {
               $("#riknar").val(ui.item.PersonBirthdayYear);
               $("#seriesdoc").val(ui.item.PersonSeriesСertificate);
               $("#numbdoc").val(ui.item.PersonNumberСertificate);
               $("#datedoc").val(ui.item.PersonDateOfIssueСertificate);
               $("#kemdoc").val(ui.item.PersonIssuedBy);
               $("#index").val(ui.item.PersonPostIndex);
               $("#oblast").val(ui.item.PersonRegion);
               $("#rayon").val(ui.item.PersonDistrict);
               $("#mssmt").val(ui.item.PersonCity);
               $("#vul").val(ui.item.PersonStreet);
               $("#bud").val(ui.item.PersonBuild);
               $("#kv").val(ui.item.PersonApartment);
               $("#tel").val(ui.item.PersonPhoneNumber);
            },
            focus:function(event,ui)
            {
               $("#riknar").val(ui.item.PersonBirthdayYear);
               $("#seriesdoc").val(ui.item.PersonSeriesСertificate);
               $("#numbdoc").val(ui.item.PersonNumberСertificate);
               $("#datedoc").val(ui.item.PersonDateOfIssueСertificate);
               $("#kemdoc").val(ui.item.PersonIssuedBy);
               $("#index").val(ui.item.PersonPostIndex);
               $("#oblast").val(ui.item.PersonRegion);
               $("#rayon").val(ui.item.PersonDistrict);
               $("#mssmt").val(ui.item.PersonCity);
               $("#vul").val(ui.item.PersonStreet);
               $("#bud").val(ui.item.PersonBuild);
               $("#kv").val(ui.item.PersonApartment);
               $("#tel").val(ui.item.PersonPhoneNumber);
            }
 
            
   
});

    $("#surname").keydown(function(event) {
     if (event.keyCode != 38 && event.keyCode !=40&& event.keyCode !=13 && event.keyCode != 9 ) {
                    $("#riknar").val('');
               $("#seriesdoc").val('');
               $("#numbdoc").val('');
               $("#datedoc").val('');
               $("#kemdoc").val('');
               $("#index").val('');
               $("#oblast").val('');
               $("#rayon").val('');
               $("#mssmt").val('');
               $("#vul").val('');
               $("#bud").val('');
               $("#kv").val('');
               $("#tel").val('');
     }
    })
    
});